package com.hut.oa.model.dao.mail;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.hut.oa.model.bean.EmailManage;
import com.hut.oa.model.bean.User;

public interface MailDao extends PagingAndSortingRepository<EmailManage, Long> {
	// 查找收件箱的邮件
	Page<EmailManage> findByGetuserAndEmEsIdAndEmIsDelete(Pageable pageable, User u, Long emEsId,Integer isdelete);
	
	@Query("from EmailManage e where  e.senduser.name like CONCAT('%',:name,'%') And e.getuser.id=:u AND  e.emEsId=0  AND e.emIsDelete=0 ")
	Page<EmailManage> findgetboxbyname(Pageable pageable,@Param("u")Long u,@Param("name")String name);
	
	@Query("from EmailManage e where  e.senduser.id=:u AND e.emEsId=:emesid  AND e.emIsDelete=0 And e.getuser.name like CONCAT('%',:name,'%')")
	Page<EmailManage> findsendboxbyname(Pageable pageable,@Param("u")Long u,@Param("name")String name,@Param("emesid")Long id);
	//通过名字查找邮件
	
	// 查看不同类型邮件 0代表收件1发件2草稿
	Page<EmailManage> findBySenduserAndEmEsIdAndEmIsDelete(Pageable pageable, User u, Long id, Integer delete);

	// 垃圾箱邮件
	@Query(" from EmailManage e  where  (e.getuser.id=:u and e.emEsId=0) and(e.emIsDelete=1) OR (e.senduser.id=:u And e.emEsId=1  OR e.emEsId=2) and(e.emIsDelete=1)")
	Page<EmailManage> findgabege(Pageable pageable, @Param("u") Long id);

	//垃圾箱通过名字插寻
	@Query(" from EmailManage e  where  (e.getuser.id=:u and e.emEsId=0) and(e.emIsDelete=1) OR (e.senduser.id=:u And e.emEsId=1  OR e.emEsId=2) and(e.emIsDelete=1) and e.emTitle like CONCAT('%',:tittle,'%')")
	Page<EmailManage> findgabegeboxbyname(Pageable pageable, @Param("u") Long id,@Param("tittle") String tittle);
	
	// 查询星标邮件
	@Query("from EmailManage e where (e.getuser.id=:u AND e.emEsId=0) and e.emStar=1 AND e.emIsDelete=0 OR(e.senduser.id=:u And e.emEsId=1  OR e.emEsId=2) and e.emStar=1 AND e.emIsDelete=0")
	Page<EmailManage> findstaremail(Pageable pageable, @Param("u") Long id);

	//星标邮件通过主题查询
	@Query("from EmailManage e where (e.getuser.id=:u AND e.emEsId=0) and e.emStar=1 AND e.emIsDelete=0  OR(e.senduser.id=:u And e.emEsId=1  OR e.emEsId=2) and e.emStar=1 AND e.emIsDelete=0 and e.emTitle like CONCAT('%',:tittle,'%')")
	Page<EmailManage> findstaremailbytittle(Pageable pageable, @Param("u") Long id,@Param("tittle") String tittle);

	// 设置星标邮件
	@Query("update  EmailManage email  set emStar=:emstar where emId=:id")
	@Modifying
	int updatesetstar(@Param("id") Long id, @Param("emstar") Integer emstar);

	// 删除邮件
	@Query("update  EmailManage email  set emIsDelete=1  where  emId=:id")
	@Modifying
	int deleteid(@Param("id") Long id);
	
	//删除记录
	@Query("delete from EmailManage where emId=:id")
	@Modifying
	int puredelete(@Param("id") Long id);
	
	//通过id和用户查找某条记录
	EmailManage findByEmIdAndSenduser(Long id,User u);
	
	EmailManage findByEmIdAndGetuser(Long id,User u);
	
	//修改已读
	@Query("update  EmailManage email  set emIsRead=1  where  emId=:id")
	@Modifying
	int isread(@Param("id")Long id);
	
	//查找未读的数量
	List<EmailManage> findByGetuserAndEmEsIdAndEmIsDeleteAndEmIsRead(User u, Long emEsId,Integer isdelete,Integer isread);
	}
